home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5887 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.6 KB  |  119 lines

  1. Path: iconet.hongkong.net!wong.yuk.wah%f18.n1000.z128
  2. From: wong.yuk.wah%f18.n1000.z128@iconet.hongkong.net (Wong Yuk Wah)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Date Arithmetic
  5. Distribution: world
  6. Message-ID: <56c8e9f056c8e9f0@iconet.hongkong.net>
  7. Date: Tue, 20 Feb 1996 22:34:00 HKT
  8. Organization: IcoNET (HONG KONG) <--> Internet gateway
  9. X-Mailer: MailGate 0.25+
  10. Reply-To:  Wong Yuk Wah <Wong.Yuk.Wah%f18.n1000.z128@iconet.hongkong.net>
  11.  
  12. At 19:01:50, on 15-02-96, mkaras@pclink.com wrote about Date Arithmetic.
  13.  
  14. I've worked out a program and hope it would be useful. This program inputs
  15. a date (today), and the number of days to be added (n), and outputs the
  16. date of n days later.
  17.  
  18.  
  19. (Cut)---------------------------------------------------------------------
  20.  
  21.  
  22. /* Date Arithmetic */
  23. #include <stdio.h>
  24.  
  25. struct date {
  26.    int day, month, year;
  27. };
  28.  
  29. typedef struct date Date;
  30.  
  31. void add_day(Date *, int);
  32.  
  33. void main(void)
  34. {
  35.    Date today;
  36.    int n;
  37.  
  38.    /* Inputs today's date and no. of days to be added */
  39.    printf("Enter today's date (dd-mm-yyyy): ");
  40.    scanf("%i%*c%i%*c%i", &today.day, &today.month, &today.year);
  41.    printf("Enter the number of days to add: ");
  42.    scanf("%d", &n);
  43.  
  44.    printf("Original date: %d-%d-%d\n", today.day, today.month, today.year);
  45.    add_day(&today, n);
  46.    printf("New date: %d-%d-%d\n", today.day, today.month, today.year);
  47. }
  48.  
  49. /* Function that adds no_of_days to Date d */
  50. void add_day(Date *d, int no_of_days)
  51. {
  52.    Date temp;  /* Date for temporary storage during calculation */
  53.    int days_of_this_month, days_of_next_month, days_to_be_added,
  54.        days_of_month[13] = {0, 31, 28, 31, 30, 31, 30,
  55.                             31, 31, 30, 31, 30, 31};
  56.  
  57.    temp.day = d->day;
  58.    temp.month = d->month;
  59.    temp.year = d->year;
  60.  
  61.    while (no_of_days > 0) {
  62.       if (temp.month == 2)
  63.          days_of_this_month = temp.year % 4 == 0 &&
  64.                               (temp.year % 100 != 0 || temp.year % 400 == 0)
  65.                               ? 29 : 28;
  66.       else
  67.          days_of_this_month = days_of_month[temp.month];
  68.  
  69.       if (temp.day < days_of_this_month) {
  70.          days_to_be_added = no_of_days < days_of_this_month - temp.day
  71.                             ? no_of_days : days_of_this_month - temp.day;
  72.          no_of_days -= days_to_be_added;
  73.          temp.day += days_to_be_added;
  74.       }
  75.       else {
  76.          temp.month++;
  77.  
  78.          if (temp.month == 2)
  79.             days_of_next_month = temp.year % 4 == 0 &&
  80.                                  (temp.year % 100 != 0 ||
  81.                                  temp.year % 400 == 0) ? 29 : 28;
  82.          else
  83.             days_of_next_month = days_of_month[temp.month];
  84.  
  85.          days_to_be_added = no_of_days < days_of_next_month
  86.                             ? no_of_days : days_of_next_month;
  87.          no_of_days -= days_to_be_added;
  88.          temp.day = days_to_be_added;
  89.       }
  90.    }
  91.  
  92.    d->day = temp.day;
  93.    d->month = temp.month;
  94.    d->year = temp.year;
  95. }
  96.  
  97.  
  98. (Cut)--------------------------------------------------------------------
  99.  
  100.  
  101. Please note that if n is too large, the performance would be
  102. unacceptable. This is because the calculation is based on month-by-month
  103. approach. If n is sufficiently large, you may modify the program to
  104. perform addition year by year. This shouldn't be a problem since the
  105. algorithm is similar.
  106.  
  107. There is another better algorithm -- stores the whole date as an int (or
  108. unsigned int) as most operating systems do. Addition and subtraction can
  109. thus be performed easily. But the problem is how to represent the date
  110. efficiently.
  111.  
  112. That's all. Hope the above text is helpful. Bye!
  113.  
  114.  
  115. Regards,
  116. Wong Yuk Wah
  117.  
  118. * Origin: IcoNET <--> Internet/Usenet gateway, 128:1000/1 (128:1000/1)
  119.